﻿using CanDoo.Admin.Core.Contracts;
using CanDoo.Core.Context;
using CanDoo.Core.Data;
using CanDoo.Data;
using CanDoo.Extensions;
using CanDoo.FineUI;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Oil.Core.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CanDoo.Oil.WebApp.cusCustomer
{
    public partial class Recharge1 : PageBase
    {
        private const string CONFIGPASSWORD = "%$@^HU^%W#G(%^G(";
        public ICustomerLevelContract CustomerLevelContract { get; set; }
        public IProductContract ProductContract { get; set; }
        public IUserContract UserContract { get; set; }
        public ICustomerContract CustomerContract { get; set; }
        public IMoneyChangeContract MoneyChangeContract { get; set; }
        public IChinaAreaContract ChinaAreaContract { get; set; }
        public IOperator Operator { get; set; }

        public override string PageCode
        {
            get
            {
                return "cusCustomer";
            }
        }

        public override string PageTitle
        {
            get
            {
                return "充值取现";
            }
        }

        public long EntityId
        {
            get
            {
                return ViewState["EntityId"].ToLong();
            }
            set
            {
                ViewState["EntityId"] = value;
            }
        }
        public override string ViewPower
        {
            get
            {
                return "cusCustomerview";
            }
        }

        public bool EnableRecharge1
        {
            get
            {
                return ViewState["EnableRecharge1"].ToBool();
            }
            set
            {
                ViewState["EnableRecharge1"] = value;
            }
        }

        protected override void PagePowerCheck()
        {
            base.PagePowerCheck();
            EnableRecharge1 = PermissionManager.HasPermission(this.PageCode + "Recharge1");
        }

        protected override void SetControlInPage()
        {
            base.SetControlInPage();
            ShowLimitError.CheckPowerWithButton(EnableRecharge1, this.btnSave);
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                EntityId = Request.QueryString["Id"].ToLong();
                LoadData();
            }
        }

        protected void LoadData()
        {
            var id = EntityId;
            Customer entity = CustomerContract.GetById(id);
            if (entity.CardType == 2)
            {
                ed_Balance.Suffix = "KG";
                ed_Money.Suffix = "KG";
                ed_Balance.SetValue(entity.Balance * 0.001);
            }
            else
            {
                ed_Balance.Suffix = "元";
                ed_Money.Suffix = "元";
                ed_Balance.SetValue(entity.Balance);
            }
            ed_Name.SetValue(entity.Name);
            ed_CardNo.SetValue(entity.CardNo);
            
        }

        protected OperationResult SaveData()
        {
            var money = ed_Money.GetValue().ToDouble();
            OperationResult or = CustomerContract.Recharge(this.EntityId,money);
            return or;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var or = SaveData();
            if(or.Succeeded)
            {
                FineUIPro.Alert.ShowInTop(or.Message);
                FineUIPro.PageContext.RegisterStartupScript(FineUIPro.ActiveWindow.GetHidePostBackReference());
            }
            else
            {
                FineUIPro.Alert.ShowInTop(or.Message);
            } 
        }
    }
}